package Cain.MkMc 
{
	import com.greensock.TweenMax;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;
	/**
	 * 全螢幕置中遮罩 v1.02 2010.11.19
	 * @author cain in liontravel 2010
	 */
	public class FullMdMk extends Sprite 
	{
		private var _root:Sprite;
		private var mdx:Number;
		private var mdy:Number;
		private var bk:Bk;
		private var sp:Sprite;
		/**
		 * 對話框
		 */
		public var coBk:CoBk;
		private var loadingMc:DisplayObject;
		/**
		 * 全螢幕置中遮罩
		 * @param	_this
		 * @param	_ww
		 * @param	_hh
		 */
		public function FullMdMk(_this:DisplayObject,_ww:Number,_hh:Number) 
		{
			_root = Sprite(_this.root);
			mdx = _ww / 2;
			mdy = _hh / 2;
			setup();
		}
		/**
		 * 設定底圖
		 * @param	_color 顏色
		 * @param	_alpha 透明度
		 */
		public function bg(_color:uint = 0, _alpha:Number = .5):void
		{
			if (bk!=null) 
			{
				sp.removeChild(bk);
			}
			bk = new Bk(100, 100, _color, _alpha);
			bk.x = -50;
			bk.y = -50;
			sp.addChild(bk);
		}
		/**
		 * 底圖進場
		 * @param	_t 淡入時間
		 */
		public function inMe(_t:Number = .5):void 
		{
			if (bk==null) 
			{
				bg();
			}
			_root.addChild(this);
			TweenMax.to(sp, _t, { autoAlpha:1 } );
		}
		/**
		 * 底圖退場
		 * @param	_t 時間
		 */
		public function remove(_t:Number = .5):void 
		{
			//_root.addChild(this);
			if (loadingMc!=null) 
			{
				if (this.contains(loadingMc)) 
				{
					removeChild(loadingMc);
				}
			}
			TweenMax.to(sp, _t, { autoAlpha:0 } );
		}
		/**
		 * 載入動畫
		 * @param	_str 文字訊息
		 * @param	_t 淡入時間
		 * @param	_class 載入顯示物件
		 */
		public function loading(_str:String = "Now loading", _t:Number = .5, _class:String = ""):void
		{
			if (loadingMc!=null) 
			{
				if (this.contains(loadingMc)) 
				{
					removeChild(loadingMc);
				}
			}
			if (_class=="") 
			{
				loadingMc = new LoadingWd(_str,false);
			}else 
			{
				var loading_class:Class = getDefinitionByName(_class) as Class;
				loadingMc = new loading_class();
			}
			addChild(loadingMc);
			inMe(_t);
		}
		/**
		 * 對話框
		 * @param	_str 對話框內文]
		 * @param	_t 進場時間
		 */
		public function tallBd(_str:String, _t:Number = .5):void 
		{
			coBk = new CoBk(_str);
			addChild(coBk);
			inMe(_t);
			coBk.addEventListener(MouseEvent.CLICK, Cobk_onCLICK);
		}
		/**
		 * 有OK的對話框
		 * @param	_str 對話框內文
		 * @param	_bn 按鈕上的文字
		 * @param	_t 進場時間
		 */
		public function talk_OkBd(_str:String, _bn:String = "確定",_t:Number = .5):void 
		{
			coBk = new CoBk_OkBn(_str,_bn);
			addChild(coBk);
			inMe(_t);
			coBk.addEventListener(MouseEvent.CLICK, Cobk_onCLICK);
		}
		private function Cobk_onCLICK(e:MouseEvent):void 
		{
			coBk.removeEventListener(MouseEvent.CLICK, Cobk_onCLICK);
			coBk.unload();
			remove();
		}
		private function setup():void
		{
			x = mdx;
			y = mdy;
			sp = new Sprite();
			sp.alpha = 0;
			sp.visible = false;
			addChild(sp);
			addEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
		}
		private function onADDED_TO_STAGE(e:Event):void 
		{
			addEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			onRESIZE();
			init();
		}
		private function init():void
		{
			addEventListeners();
		}
		
		private function addEventListeners():void
		{
			_root.stage.addEventListener(Event.RESIZE, onRESIZE);
		}
		
		private function onRESIZE(e:Event = null):void 
		{
			sp.width = _root.stage.stageWidth;
			sp.height = _root.stage.stageHeight;
		}
		private function removeEventListeners():void
		{
			_root.stage.removeEventListener(Event.RESIZE, onRESIZE);
		}
		private function onREMOVED_FROM_STAGE(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
			removeEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			removeEventListeners();
		}
	}
	
}